/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package saltocaballo.Interfaces;

import de.javasoft.plaf.synthetica.SyntheticaBlackMoonLookAndFeel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import saltocaballo.comunes.Configuracion;

/**
 *
 * @author Martinoxs
 */
public class tableroResultado extends javax.swing.JFrame {

    Font fuente = new Font("Arial", Font.BOLD, 12);
    /**
     * Creates new form tableroResultado
     */
    private tableroResultado() {
        initComponents();
    }

    public tableroResultado(String rdo, int filas, int columnas, String casillaInicio) {
        initComponents();
        cargarCasillas(filas, columnas, casillaInicio);
        String[] movs = rdo.split("Movimiento");
        Map<String,String> casMovs = new HashMap<String, String>();
        
        for(String s: movs){
            if (s.startsWith("(")){
                String[] casilla = s.split(":");
                casMovs.put(casilla[1].trim(), eliminarParentesis(casilla[0]).trim());
            }
        }
        
        for(Component c: panelCasillas.getComponents()){
            if(c.getClass().equals(JTextField.class)){
                JTextField text = (JTextField) c;
                if (text.getText().isEmpty() && !text.getText().equals("0")){
                    text.setText(casMovs.get(text.getName()));
                    text.setFont(fuente);
                    text.setHorizontalAlignment(JTextField.CENTER);
                }
            }
        }
        
        for(Component c: panelCasillas.getComponents()){
            if(c.getClass().equals(JTextField.class)){
                JTextField text = (JTextField) c;
                if(text.getText().isEmpty()){
                    text.setText("X");
                    text.setFont(fuente);
                    text.setHorizontalAlignment(JTextField.CENTER);
                }
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCerrar = new javax.swing.JButton();
        panelCasillas = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Tablero Resultado");

        btnCerrar.setText("Cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        panelCasillas.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout panelCasillasLayout = new javax.swing.GroupLayout(panelCasillas);
        panelCasillas.setLayout(panelCasillasLayout);
        panelCasillasLayout.setHorizontalGroup(
            panelCasillasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panelCasillasLayout.setVerticalGroup(
            panelCasillasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 338, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 451, Short.MAX_VALUE)
                        .addComponent(btnCerrar))
                    .addComponent(panelCasillas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelCasillas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCerrar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCerrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                try {
                    UIManager.setLookAndFeel(new SyntheticaBlackMoonLookAndFeel());
                } catch (ParseException ex) {
                    if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                    }
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                final JFrame uiApp = new tableroResultado();
                BufferedImage image = null;
                try {
                    image = ImageIO.read(uiApp.getClass().getResource("/iconos/logo.png"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JPanel panelCasillas;
    // End of variables declaration//GEN-END:variables

    private void cargarCasillas(int filas, int columnas, String casillaInicio) {
        JTextField casilla;
        
        int contadorI = 0;
        int contadorJ = 0;
        
        for(int i = 0; i < columnas; i++){
            for(int j = 0; j < filas; j++){
                casilla = new JTextField();
                casilla.setName(String.valueOf(j).concat(";").concat(String.valueOf(i)));
                if (casillaInicio.equals(casilla.getName())){
                    casilla.setText("0");
                    casilla.setFont(fuente);
                    casilla.setHorizontalAlignment(JTextField.CENTER);
                }
                casilla.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                casilla.setPreferredSize(new Dimension(Configuracion.getInstancia().getTamanioCasilla(),Configuracion.getInstancia().getTamanioCasilla())); 
                casilla.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                casilla.setBounds(contadorI, contadorJ, Configuracion.getInstancia().getTamanioCasilla(), Configuracion.getInstancia().getTamanioCasilla());
                casilla.setEditable(false);
                contadorJ += Configuracion.getInstancia().getTamanioCasilla();
                
                casilla.setVisible(true);
                casilla.repaint();
                
                panelCasillas.add(casilla);
                panelCasillas.updateUI();
            }
            contadorI += Configuracion.getInstancia().getTamanioCasilla();
            contadorJ = 0;
        }
        
    }

    private String eliminarParentesis(String string) {
        String s = string.substring(1);
        s = s.substring(0, s.length() - 1);
        return s;
    }
}
